<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Account extends Backend {

	public function __construct()
	{
		parent::__construct();
		$this->load->model(['site_model','adword/adaccount_model','adword/adaccountreport_model']);
		$this->data['page_home']  = lang('lang_dashboard');
		$this->data['page_title'] = "谷歌广告账户报表";
        $this->data['curreny'] = ["USD"=>"$","CNY"=>"¥"];
	}

    public function index()
    {


        $timelimit=$this->input->get("timelimit");

		if(!$timelimit)
		{
			$timelimit=[
				"start"=>date("Y-m-d",strtotime("-1 days")),
				"end"=>date("Y-m-d",strtotime("-1 days")),
			];
        }

        $accountmap = $this->adaccount_model->getaccountmap();
        $result = $this->adaccountreport_model->getrangelist($timelimit);
        $this->data["result"]=$result;
        $this->data["map"]=$accountmap;

        $this->data["timerange"]=$timelimit;
        $this->data['content_header_title'] = content_header_title($this->data['page_title']);
		$this->data['content'] = 'adword/account';

		$this->render();
	}
	

	public function ajaxbind()
	{
		$accid=$this->input->post("accid");
		$siteid=$this->input->post("siteid");
		$this->adaccount_model->update($accid,["siteid"=>$siteid]);
		die("ok");
	}

	public function bind()
	{

		$this->data['page_title']="绑定广告账号";
		
		$mapsite=[];
		$sites=$this->site_model->getallsync();
		foreach($sites as $item)
		{
			$mapsite[$item["id"]]=$item;
		}
		$this->data["sites"]=$mapsite;

		$accountmap = $this->adaccount_model->getaccountmap();
		$this->data["map"]=$accountmap;

		$this->data['content_header_title'] = content_header_title($this->data['page_title']);
		$this->data['content'] = 'adword/bind';

		$this->render();
	}


	public function detail($accid)
	{

		$acc=$this->adaccount_model->getitembykey($accid);
		$this->data["account"]=$acc;

		$timelimit=$this->input->get("timelimit");

		if(!$timelimit)
		{
			$timelimit=[
				"start"=>date("Y-m-d",strtotime("-30 days")),
				"end"=>date("Y-m-d",strtotime("-1 days")),
			];
		}
		
		$result = $this->adaccountreport_model->getdetail($accid,$timelimit);
		$this->data["result"]=$result;
		$this->data["timerange"]=$timelimit;
		
		$accountmap = $this->adaccount_model->getaccountmap();
		$this->data["map"]=$accountmap;

		$this->data['content_header_title'] = content_header_title($this->data['page_title']);
		$this->data['content'] = 'adword/detail';
		$this->render();

	}
}

